import requests
import base64

def image_to_base64(file_path):
    """
    将指定路径的图片文件读取并转换为 Base64 编码的字符串。
    
    :param file_path: 图片文件的本地路径（例如 "test.jpg"）
    :return: Base64 编码的字符串
    """
    try:
        with open(file_path, "rb") as image_file:
            encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
        return encoded_string
    except FileNotFoundError:
        print(f"错误：文件 {file_path} 未找到。")
    except Exception as e:
        print(f"读取图片时发生错误：{e}")


# 将图片转为 Base64
image_base64 = image_to_base64("./test/test.png")

url = "http://127.0.0.1:5000/api/conversation"

# 请求头参数
headers = {
    "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTc1MzYxNzA2MCwianRpIjoiMjJmMWY5ZmUtYTM4MC00MzQ0LWJmZmQtMjExYzc0NzgxMjRmIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6ImVhOTI1OTViLTBjMDEtNGI3MS1iMDE3LTI0YWY5NDI3NGMzZiIsIm5iZiI6MTc1MzYxNzA2MCwiY3NyZiI6IjQyNGFjMzQ1LWEwZmUtNGEzMy1hOTViLWQzMDBjZWI4ZjE3MyIsImV4cCI6MTc1MzYyNDI2MH0.RmDVVuvyC7I9mUfLNI3mbdEiGqsJbrFZhmljxEgiU4A",  # 替换为实际 Token
    "Content-Type": "application/json; charset=utf-8"
}

# 构造请求体
data = {
    "conversation_id": "web_afa9ae91-aa12-4f8a-a9e7-66bd752ab2ab",
    "message": "描述一下这张图片",
    "image_base64": image_base64,
    "model_key" : "Qwen/Qwen2.5-VL-72B-Instruct",
}

# 发送请求（与之前示例一致）
response = requests.post(url, json=data, headers=headers)
print(response)